import 'package:flutter/services.dart';

class MethodName {
  static const defaultChannelName = 'CRM_FLUTTER_CHANNEL_DEFAULT'; //通道名称

  static const test = "test"; //test
}

class MyMethodChannel {
  late MethodChannel channel;

  MyMethodChannel._privateConstructor() {
    channel = const MethodChannel(MethodName.defaultChannelName);
    channel.setMethodCallHandler(methodCallback);
    print("YouLuMethodChannel instance");
  }

  Future<dynamic> methodCallback(MethodCall methodCall) async {
    switch (methodCall.method) {
      case 'test':
        print(
            "原生Android调用了flutterMethod方法：${methodCall.arguments.toString()}");
        break;
    }
  }

  static final MyMethodChannel _instance =
      MyMethodChannel._privateConstructor();

  static MyMethodChannel get instance {
    return _instance;
  }

  ///获取通道
  MethodChannel getMethodChannel() {
    return channel;
  }

  ///测试native的test方法
  void callNativeTest() {
    channel.invokeMethod(MethodName.test, {'name': '张三', 'gender': 1});
  }
}
